import sys
import json
import os
from ccpm.pmbase import PluginBase
from ccpm.glan import Lan
from ccpm.constants import Constant
from ccpm.conffac import PackConf


class InitcfgPlugin(PluginBase):

    PLUGIN_INFO = dict(
        name="initcfg",
        help="init ccpm package config file in the path!"
    )

    def __init__(self):
        self._inc = super().__init__(self.PLUGIN_INFO)

    def __chk_localconff(self):
        conff = os.path.join(os.getcwd(), Constant.PACKINFO_FILE)
        if os.path.isfile(conff):
            self.err_break(
                Lan.p(self.name, "ERR_ALREADY_PACKJSON",
                      Constant.PACKINFO_FILE))

    def parser_config(self, argparser):
        super().parser_config(argparser)
        argparser.add_argument(
            "-i", dest="isfull", help="if init the full-fmt cnfig file in curent path")

    def run(self, argpars):
        super().run(argpars)
        self.__chk_localconff()
        conf = PackConf().get_require_conf()
        is_full = argpars.isfull
        if is_full:
            conf = PackConf().get_full_conf()

        with open(Constant.PACKINFO_FILE, "w") as fp:
            fp.write(json.dumps(conf_template, sort_keys=True, indent=4))
        self.logger.info(
            Lan.p(self.name, "INFO_CRTCONF_SUC", Constant.PACKINFO_FILE))


__all__ = [
    "InitcfgPlugin"
]
